//
//  myPlantController.m
//  iOS_NongLian
//
//  Created by n369 on 16/3/4.
//  Copyright © 2016年 NongLian. All rights reserved.
//

#define CGRectFromLeft CGRectMake(0, 260, self.view.frame.size.width / 2, 0)
#define CGRectToLeft CGRectMake(0, 260, self.view.frame.size.width / 2, 44 * 5)
#define CGRectFromRight CGRectMake(self.view.frame.size.width / 2, 260, self.view.frame.size.width / 2, 0)
#define CGRectToRight CGRectMake(self.view.frame.size.width / 2, 260, self.view.frame.size.width / 2, 44 * 8)

#import "myPlantController.h"
#import "PlantCollectionCell.h"

@interface myPlantController () <UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate> {
    UICollectionView * _collectionView;
    NSMutableArray * _dataSource;
    BOOL _edit;
    //-------------------------添加作物
    NSArray * _arr;
    NSMutableArray * _rightArr;
    UITableView * _leftTableView;
    UITableView * _rightTableView;
    UILabel * _text;
}

@end

@implementation myPlantController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavigationBar:9];
    self.view.backgroundColor = [UIColor whiteColor];
    self.back.text = @"我关注的城市";
    
    [self initData];
    [self createCollectionView];
    [self createTableView];
}

- (void)initData {
    _edit = 0;
    _dataSource = [[NSMutableArray alloc] initWithArray:@[@"", @"", @"", @"", @"", @"", @""]];
    
    //-------------------------添加作物
    _arr = @[@{@"全部":@[@"棉花", @"小麦", @"水稻", @"甘蔗", @"葡萄", @"玉米", @"花生", @"葡萄", @"玉米", @"花生"]}, @{@"大田作物":@[@"小麦", @"水稻", @"甘蔗"]}, @{@"经济作物":@[@"棉花", @"小麦", @"水稻", @"甘蔗"]}, @{@"蔬菜作物":@[@"小麦", @"水稻", @"甘蔗", @"葡萄", @"玉米"]}, @{@"水果作物":@[@"葡萄", @"玉米", @"花生"]}];
    _rightArr = [[_arr[0] allValues] firstObject];
}

- (void)createTableView {
    _text = [Factory createLabelWithTitle:@"选择您希望关注的作物" frame:CGRectMake(0, 230, ScreenWidth, 30) fontSize:12];
    _text.textAlignment = NSTextAlignmentCenter;
    _text.textColor = RGB(102, 102, 102);
    _text.backgroundColor = RGB(237, 237, 237);
    _text.alpha = 0;
    _text.layer.borderColor = RGB(216, 216, 216).CGColor;
    _text.layer.borderWidth = 1.f;
    [self.view addSubview:_text];
    
    _leftTableView = [[UITableView alloc] initWithFrame:CGRectFromLeft];
    _leftTableView.delegate = self;
    _leftTableView.dataSource = self;
    [self.view addSubview:_leftTableView];
    _rightTableView = [[UITableView alloc] initWithFrame:CGRectFromRight];
    _rightTableView.delegate = self;
    _rightTableView.dataSource = self;
    [self.view addSubview:_rightTableView];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 15.f;
    layout.minimumInteritemSpacing = (ScreenWidth - 40 - 4 * 50) / 3;
    layout.itemSize = CGSizeMake(48, 60);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 64, ScreenWidth - 20, ScreenHeight - 64) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[PlantCollectionCell class] forCellWithReuseIdentifier:@"collectionCell"];
}

#pragma mark - datasource&delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PlantCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    if (indexPath.item != _dataSource.count) {
        [cell setImage:@"PlantIcon" andText:@"城市名"];
        cell.deleteImage.alpha = _edit?1:0;
    } else {
        [cell setImage:@"AddGreenIcon" andText:@"添加"];
        cell.deleteImage.alpha = 0;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%2ld,___%2ld",indexPath.item,indexPath.section);
    if (!_edit) {
        if (indexPath.item == _dataSource.count) {
            _edit = 1;
            [self ToAction];
            [_collectionView reloadData];
        } else {
//            [self.navigationController pushViewController:[NlianNongYaoController new] animated:YES];
            //----------------------------------------跳转之后数据要加载
        }
    } else {
        if (indexPath.item == _dataSource.count) {
            _edit = 0;
            [self FromAction];
            [_collectionView reloadData];
        } else {
            //----------------------------------------需要移除数据源
            [_dataSource removeObjectAtIndex:indexPath.item];
            [_collectionView reloadData];
        }
    }
}

- (void)ToAction {
    [_collectionView setContentOffset:CGPointMake(0, (((_dataSource.count > 4) ? _dataSource.count : 4) / 4 - 1) * 75) animated:YES];
    [UIView animateWithDuration:0.3 animations:^{
        _collectionView.frame = CGRectMake(10, 64, ScreenWidth - 20, 160);
        _leftTableView.frame = CGRectToLeft;
        _rightTableView.frame = CGRectToRight;
        _text.alpha = 1;
    }];
}

- (void)FromAction {
    [UIView animateWithDuration:0.3 animations:^{
        _collectionView.frame = CGRectMake(10, 64, ScreenWidth - 20, ScreenHeight - 64);
        _leftTableView.frame = CGRectFromLeft;
        _rightTableView.frame = CGRectFromRight;
        _text.alpha = 0;
    }];
}

#pragma mark tableViewDelegate&datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _rightTableView) {
        return _rightArr.count;
    }
    return _arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"defaultCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"defaultCell"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:12.f];
    cell.textLabel.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
    
    if (tableView == _leftTableView) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = [[_arr[indexPath.row] allKeys] firstObject];
        cell.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
        if (indexPath.row == 0) {
            cell.backgroundColor = [UIColor whiteColor];
        }
    } else {
        cell.textLabel.text = _rightArr[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _leftTableView) {
        for (int i = 0; i < 5; i++) {
            UITableViewCell * cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            if (i == indexPath.row) {
                cell.backgroundColor = [UIColor whiteColor];
            } else {
                cell.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
            }
        }
        _rightArr = [[_arr[indexPath.row] allValues] firstObject];
        if (_rightArr.count < 9) {
            _rightTableView.frame = CGRectMake(self.view.frame.size.width / 2, 260, self.view.frame.size.width / 2, 44 * _rightArr.count);
        } else {
            _rightTableView.frame = CGRectMake(self.view.frame.size.width / 2, 260, self.view.frame.size.width / 2, 44 * 8);
        }
        [_rightTableView reloadData];
    } else {
        NSLog(@"%@", _rightArr[indexPath.row]);
        if (_dataSource.count == 27) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            return;
        }
        [_dataSource addObject:@""];
        [_collectionView reloadData];
        if ((_dataSource.count ) % 4 == 0) {
            [_collectionView setContentOffset:CGPointMake(0, ((_dataSource.count +1) / 4 - 1) * 75) animated:YES];
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backTitleAction {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
